package com.sjh.lib_app.router

import android.net.Uri
import androidx.core.os.bundleOf
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ActivityUtils
import com.sjh.baseutils.loge
import com.sjh.baseutils.toast
import com.sjh.lib_app.router.SchemeFilterActivity.Companion.host
import com.sjh.lib_app.router.SchemeFilterActivity.Companion.scheme

/**
 *
 * @author Extends
 * @date 2019/6/14/014
 */

/**
 *  路由跳转
 *
 *     // 转场动画(常规方式)
        ARouter.getInstance()
        .build("/test/activity2")
        .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
        .navigation(this);

        // 转场动画(API16+)
        ActivityOptionsCompat compat = ActivityOptionsCompat.
        makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);

        // ps. makeSceneTransitionAnimation 使用共享元素的时候，需要在navigation方法中传入当前Activity
 */
fun route(routePath:String?,vararg params: Pair<String, Any?>,callback: NavigationCallback?=null,
          requestCode:Int=-1 ,extend:Postcard.()->Postcard = {this}){
    try {
        if(routePath.isNullOrBlank()){
            return
        }
        ARouter.getInstance().build(routePath)
                .with(bundleOf(*params))
                .extend()
                .navigation(ActivityUtils.getTopActivity(),requestCode,callback)
    }catch (e:Exception){
        e.printStackTrace()
    }
}
fun routeFromUri(routePath:String?, vararg params: Pair<String, Any?>, callback: NavigationCallback?=null,
                 requestCode:Int=-1, extend:Postcard.()->Postcard = {this}){
    if(routePath.isNullOrBlank()){
        return
    }
    //如果以http开头，就直接跳转到网页界面
    val rp = if(routePath.startsWith("http")){
        val path = "/app/web"
        "$path?url=$routePath"
    }else routePath
    try {
        ARouter.getInstance().build(Uri.parse("$scheme://$host$rp"))
            .with(bundleOf(*params))
            .extend()
            .navigation(ActivityUtils.getTopActivity(),requestCode,callback)
    }catch (e : Exception){
        e.printStackTrace()
        "网络异常".toast()
    }

}
/**
 * 路由扩展值
 */
object RouteExtras{
    /**
     * 默认
     */
    const val DEFAULT :Int = 0b10000000000000000000000000000000.toInt()

    /**
     * 检查是否登录
     */
    const val CHECK_LOGIN : Int = 0b01000000000000000000000000000000

//    /**
//     * 检查是否实名认证
//     */
//    const val CHECK_CERTIFICATION : Int = 0b00100000000000000000000000000000
//
//    /**
//     * 检查是否有相机权限
//     */
//    const val CHECK_CAMERA_PERMISSION : Int = 0b00010000000000000000000000000000
//
//    /**
//     * 检查是否有高级实名认证的权限
//     */
//    const val CHECK_SIGN_PERMISSION : Int = 0b00001000000000000000000000000000
}